#include "pseRobotCacheer.h"

PseRobotCacheer::PseRobotCacheer() : size(0), expired_time(0){
}

PseRobotCacheer::~PseRobotCacheer(){
}

int PseRobotCacheer::robot_cached(char *ip, int ip_len, const char *robot_filename){	
    assert(ip != NULL);
    assert(ip_len >= 0);
    assert(robot_filename != NULL);
    //char *robot_str = NULL;
    FILE *robot_fp = fopen(robot_filename, "r");
    long  lsize = 0;
    if(robot_fp == NULL){
        //printf("could not open robot file\n");
        exit(1);
    }
    else{
        /* obtained file size */
        fseek(robot_fp, 0, SEEK_END);
        lsize = ftell(robot_fp);
        rewind(robot_fp);
        char  *robot_buf = NULL;
        
        /* allocate memory to contain the whole file*/
        robot_buf = (char *)malloc(sizeof(char) *lsize);
        if (robot_buf == NULL) {
            fputs ("allocate robot Memory of file buffer error",stderr); 
            exit (1);
        }
        
        /* copy the file into buffer */
        size_t robot_result = fread(robot_buf, 1, lsize, robot_fp);
        if(robot_result != (size_t)lsize){
            fputs("reading error", stderr);
            exit(1);           
        }
        else{
            //printf("robot_buf:%s\n", (char *)robot_buf);        
            if(insert(ip, ip_len, (void *)robot_buf, strlen(robot_buf)) != 0){
                //printf("cacheer insert is failed\n");
                return -1;
            }
        }
        fclose(robot_fp);
        free(robot_buf);
        return 0;    
    }
    return -1;
}

bool PseRobotCacheer::dump_report(char *cache_name, char* filename){
    assert(filename != NULL);
    report(cache_name, filename);
    return false;
}

bool PseRobotCacheer::dump_file(char *filename){
    return false;
}

bool PseRobotCacheer::load_file(const char* filename){
    assert(filename != NULL);
    if((load_file(filename)) == true){
        return true;
    }
    return false;
}
